package com.heima.article.controller;

import com.heima.article.service.ApArticleService;
import com.heima.common.dtos.PageResult;
import com.heima.common.dtos.ResponseResult;
import com.heima.model.article.dtos.ArticleDto;
import com.heima.model.article.dtos.ArticleHomeDto;
import com.heima.model.media.dtos.WmNewsResultDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class ApArticleController {

    @Autowired
    private ApArticleService apArticleService;
    /**
     * 保存文章
     * @param dto
     * @return
     */
    @PostMapping("/api/v1/article/save")
    public Long save(@RequestBody WmNewsResultDTO dto){
        return apArticleService.saveArticle(dto);
    }

    /**
     * 文章首页
     * @param dto
     * @return
     */
    @PostMapping("/api/v1/article/load")
    public ResponseResult<List<ArticleDto>> load(@RequestBody ArticleHomeDto dto){
        return ResponseResult.ok(apArticleService.load(dto,1));
    }

    /**
     * 下拉，获取最新
     * @param dto
     * @return
     */
    @PostMapping("/api/v1/article/loadnew")
    public ResponseResult<List<ArticleDto>> loadNew(@RequestBody ArticleHomeDto dto){
        return ResponseResult.ok(apArticleService.load(dto,1));
    }

    /**
     * 上拉获取更多
     * @param dto
     * @return
     */
    @PostMapping("/api/v1/article/loadmore")
    public ResponseResult<List<ArticleDto>> loadMore(@RequestBody ArticleHomeDto dto){
        return ResponseResult.ok(apArticleService.load(dto,2));
    }

    /**
     * 分页查询 文章数据
     * @param page
     * @param size
     * @return
     */
    @GetMapping("/api/v1/article/findByPage")
    public List<ArticleDto> findByPage(@RequestParam("page")Integer page,
                                       @RequestParam("size")Integer size){
        return apArticleService.findByPage(page,size);
    }
}
